/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import OBJECT.DangKyLamViec;
import OBJECT.LichTrinh;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Steven
 */
public class DangKyLamViecDAO {
    
    private Connect connect = null;
    public DangKyLamViecDAO(){
        
    }
    
    public DangKyLamViecDAO(String User ,String Pass,String port,String DataBase,String Host){
        connect = new Connect(User, Pass, port, DataBase, Host);
    }
    
    public List<DangKyLamViec> LayDanhSachDangKyLamViec(){
        CallableStatement cals = null;
        java.sql.Connection conn = null;
        ResultSet rs = null;
        List<DangKyLamViec> ds = new ArrayList<DangKyLamViec>();
        try {
//            conn = util.Connection.getConnection();
            conn = connect.GetConnect();
            cals = conn.prepareCall("{call sp_ThongTinDangKyLichLamViec()}");
            rs = cals.executeQuery();
            DangKyLamViec dklv = null;
            LichTrinh lt = null;
            while(rs.next()){
                dklv = new DangKyLamViec();
                dklv.setMaDangKyLamViec(rs.getInt("Ma_DkiLamViec"));
                dklv.setMaNhanVien(rs.getInt("Ma_NV"));
                dklv.setMaLichVanChuyen(rs.getInt("Ma_VC"));
                dklv.setNgayDangKy(rs.getDate("NgayDangKi"));
                
                dklv.setTenTuyenDuong(rs.getString("Ten_TuyenDuong") );
                dklv.setTenTau(rs.getString("TenTau"));
                
                dklv.setThoiGianKhoiHanh(rs.getDate("ThoiGianKhoiHanh"));
                dklv.setThoiGianDen(rs.getDate("ThoiGianDen"));
                
                dklv.setTenNhanVien(rs.getString("Ten_NV"));
                ds.add(dklv);
                if(dklv.getMaDangKyLamViec() > DangKyLamViec.getCurrentMaxId()){
                    DangKyLamViec.setCurrentMaxId(dklv.getMaDangKyLamViec());
                }
            }
        } catch (Exception e) {
            
            System.err.println(e.getMessage());
        }finally{
             try {
                 if(rs != null){
                     rs.close();
                 }
                 cals.close();
                 CloseConnect();
             } catch (Exception e) {
             }
         }
        return ds;
    }
    
    public String DangKyLichLamViec(int maNhanVien, int maLichVanChuyen, java.sql.Date ngayDangKy){
        CallableStatement cals = null;
        java.sql.Connection conn = null;
        String err = "";
        try {
//            conn = util.Connection.getConnection();
            conn = connect.GetConnect();
            cals = conn.prepareCall("{call sp_DangKyLichLamViec(?,?,?,?)}");
            cals.setInt("Ma_NV", maNhanVien);
            cals.setInt("Ma_VC", maLichVanChuyen);
            cals.setDate("NgayDangKy", ngayDangKy);
            cals.registerOutParameter("err", Types.NVARCHAR);
            cals.setString("err", err);
            cals.execute();
            err = cals.getString("err");
        } catch (Exception e) {
            err = e.getMessage();
            System.err.println(e.getMessage());
        }finally{
             try {
                 
                 cals.close();
                 CloseConnect();
             } catch (Exception e) {
             }
         }
        return err;
    }

    public String HuyLichLamViec(int maNhanVien, int maVanChuyen){
        CallableStatement cals = null;
        java.sql.Connection conn = null;
        String kq = "";
        try {
//            conn = util.Connection.getConnection();
            conn = connect.GetConnect();
            cals = conn.prepareCall("{call sp_XoaLichLamViec(?,?,?)}");
            cals.setInt(1, maNhanVien);
            cals.setInt(2, maVanChuyen);
            cals.registerOutParameter("err", Types.NVARCHAR);
            cals.setString(3, kq);
            cals.execute();
            kq = cals.getString("err");
        } catch (Exception e) {
            kq = e.getMessage();
            System.err.println(kq);
        }finally{
             try {
                 
                 cals.close();
                 CloseConnect();
             } catch (Exception e) {
             }
         }
        return kq;
    }
    public void CloseConnect()
      {
          try
          {
          connect.Close();
          }catch(Exception e)
          {
              ;
          }
      } 
}
